package org.algorithm.熊猫.数组.三月.第四周;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhx
 * @version 1.0
 * @date 2022/03/24 8:38 PM
 * @description: 周三
 * <p>
 * <p>
 * <p>
 * url:  没有题， 群里现出的
 */
public class 矩阵打印 {

    /****
     *   二维数组如下：
     *                      1       2       3
     *
     *                      4       5       6
     *
     *                      7       8       9
     *
     *
     *  要求：   打印顺序为：    1 -> 2 -> 3 -> 6 -> 9 -> 8 -> 7 -> 4 -> 5
     */

    public static void main(String[] args) {
        int[][] arr = {{1, 2, 3,4,5,6,7,8,9,10}, {11,12,13,14,15,16,17,18,19,20}, {21,22,23,24,25,26,27,38,39,40}};

        System.out.println(printInt(arr));
    }

    private static List<Integer> printInt(int[][] arr) {
        List<Integer> result = new ArrayList<>();
        List<List<Integer>> transf = transf(arr);
        printJust(transf, true, result);
        return result;
    }

    private static void printJust(List<List<Integer>> arr, boolean flag, List<Integer> result) {
        if (arr == null || arr.size() <= 0) {
            return;
        }
        if (flag) {
            if (arr.get(0).size() > 0) {
                result.addAll(arr.get(0));
                arr.remove(0);
                printEveryHead(arr, false, result);
            }
        } else {
            int len = arr.size() - 1;
            List<Integer> ints = arr.get(len);
            if (ints.size() > 0) {
                for (int i = ints.size()-1; i >= 0; i--) {
                    result.add(ints.get(i));
                }
                arr.remove(len);
                printEveryHead(arr, true, result);
            }
        }

    }

    private static void printEveryHead(List<List<Integer>> arr, boolean flag, List<Integer> result) {
        if (arr == null || arr.size() <= 0) {
            return;
        }
        if (flag) {
            int len = arr.size() - 1;
            for (int i = len; i >= 0; i--) {
                result.add(arr.get(i).remove(0));
            }
            printJust(arr,true,result);
        } else {
            for (List<Integer> list : arr) {
                result.add(list.remove(list.size() - 1));
            }
            printJust(arr,false,result);
        }
    }

    private static List<List<Integer>> transf(int[][] arr) {
        List<List<Integer>> result = new ArrayList<>();
        for (int[] ints : arr) {
            List<Integer> list = new ArrayList<>();
            for (int anInt : ints) {
                list.add(anInt);
            }
            result.add(list);
        }
        return result;
    }
}
